import { PrismaClient } from '@prisma/client';
import { addDays } from 'date-fns';
import { hash } from 'bcryptjs';

const prisma = new PrismaClient();

// 使用 const enum 来避免命名冲突
const enum Constants {
  ADMIN_ID = 'cm76c4fx70000i6tj9hls1usx'
}

// 添加类型定义
interface Activity {
  title: string;
  description: string;
  duration: string;
  date: Date;
}

interface Destination {
  name: string;
  description: string;
  imageUrl: string;
  metadata: {
    location: string;
    bestTimeToVisit: string;
    tips: string;
    category: string;
  };
  activities: Activity[];
}

const officialDestinations: Destination[] = [
  {
    name: "故宫博物院",
    description: "中国明清两代的皇家宫殿，世界上现存最大的宫殿建筑群，收藏了大量珍贵文物。",
    imageUrl: "/images/destinations/forbidden-city.jpg",
    metadata: {
      location: "北京市东城区景山前街4号",
      bestTimeToVisit: "3月-5月，9月-11月",
      tips: "建议提前在网上预约门票，旺季需要提前7天。参观时间约3-4小时。",
      category: "文化古迹"
    },
    activities: [
      {
        title: "太和殿参观",
        description: "参观明清两代皇帝举行大典的正殿，感受皇家气派。",
        duration: "1小时",
        date: new Date()
      },
      {
        title: "珍宝馆展览",
        description: "欣赏皇室珍贵文物，了解宫廷文化。",
        duration: "1.5小时",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "御花园游览",
        description: "游览清代皇家园林，欣赏传统园林艺术。",
        duration: "45分钟",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "西湖",
    description: "杭州市标志性景点，以优美的湖光山色和众多的人文古迹闻名于世。",
    imageUrl: "/images/destinations/west-lake.jpg",
    metadata: {
      location: "浙江省杭州市西湖区",
      bestTimeToVisit: "全年皆宜，春秋最佳",
      tips: "建议游览2-3天，可以租自行车环湖，清晨或傍晚景色最美。",
      category: "自然风光"
    },
    activities: [
      {
        title: "苏堤春晓",
        description: "漫步苏堤，欣赏西湖十景之一。",
        duration: "2小时",
        date: new Date()
      },
      {
        title: "雷峰塔游览",
        description: "登塔远眺西湖全景，了解白蛇传传说。",
        duration: "1小时",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "船游西湖",
        description: "乘船游览西湖，欣赏湖光山色。",
        duration: "45分钟",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "黄山",
    description: "安徽省著名山岳景区，以奇松、怪石、云海、温泉闻名。",
    imageUrl: "/images/destinations/huangshan.jpg",
    metadata: {
      location: "安徽省黄山市黄山区",
      bestTimeToVisit: "4月-10月",
      tips: "建议住宿山上看日出，准备防寒衣物，旺季需提前预订住宿。",
      category: "自然风光"
    },
    activities: [
      {
        title: "观日出",
        description: "在光明顶观赏黄山日出,体验云海变幻。",
        duration: "2小时",
        date: new Date()
      },
      {
        title: "探访迎客松",
        description: "参观黄山标志性景观迎客松,了解黄山松文化。",
        duration: "1小时",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "温泉体验",
        description: "在黄山温泉放松身心,缓解登山疲劳。",
        duration: "2小时",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "丽江古城",
    description: "世界文化遗产，保存完好的纳西族古城，体现了中国古代城市建设的成就。",
    imageUrl: "/images/destinations/lijiang.jpg",
    metadata: {
      location: "云南省丽江市古城区",
      bestTimeToVisit: "3月-10月",
      tips: "建议体验纳西族文化，品尝当地美食，注意防晒和高原反应。",
      category: "文化古迹"
    },
    activities: [
      {
        title: "四方街漫步",
        description: "在古城中心四方街感受纳西古城风情。",
        duration: "2小时",
        date: new Date()
      },
      {
        title: "纳西古乐欣赏",
        description: "欣赏纳西族传统音乐表演。",
        duration: "1.5小时",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "束河古镇游览",
        description: "探访古城旁的束河古镇,体验更安静的古镇氛围。",
        duration: "3小时",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "九寨沟",
    description: "世界自然遗产，以彩池、瀑布、雪山、森林闻名于世。",
    imageUrl: "/images/destinations/jiuzhaigou.jpg",
    metadata: {
      location: "四川省阿坝藏族羌族自治州九寨沟县",
      bestTimeToVisit: "9月-10月",
      tips: "建议游览2-3天，准备保暖衣物，注意高原反应。",
      category: "自然风光"
    },
    activities: [
      {
        title: "诺日朗瀑布观赏",
        description: "欣赏九寨沟最大的钙化瀑布。",
        duration: "1小时",
        date: new Date()
      },
      {
        title: "五彩池游览",
        description: "观赏九寨沟最著名的彩池之一。",
        duration: "45分钟",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "原始森林徒步",
        description: "体验原始森林的自然风光。",
        duration: "2小时",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "敦煌莫高窟",
    description: "世界文化遗产，收藏了大量佛教艺术珍品，是中国佛教艺术的瑰宝。",
    imageUrl: "/images/destinations/dunhuang.jpg",
    metadata: {
      location: "甘肃省敦煌市莫高窟路",
      bestTimeToVisit: "5月-10月",
      tips: "需要提前预约，建议请专业导游讲解，禁止拍照。",
      category: "文化古迹"
    },
    activities: [
      {
        title: "洞窟艺术欣赏",
        description: "参观精选洞窟，了解佛教艺术发展史。",
        duration: "2.5小时",
        date: new Date()
      },
      {
        title: "数字展厅体验",
        description: "通过数字技术深入了解壁画细节。",
        duration: "1小时",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "敦煌艺术讲解",
        description: "聆听专业讲解员介绍敦煌艺术特色。",
        duration: "1小时",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "三亚亚龙湾",
    description: "中国最美的海湾之一，拥有洁白的沙滩和清澈的海水。",
    imageUrl: "/images/destinations/yalong-bay.jpg",
    metadata: {
      location: "海南省三亚市亚龙湾国家旅游度假区",
      bestTimeToVisit: "11月-次年4月",
      tips: "可以体验水上运动，注意防晒，建议选择合适的度假酒店。",
      category: "海滨度假"
    },
    activities: [
      {
        title: "海滩休闲",
        description: "在白沙滩享受阳光与海浪。",
        duration: "3小时",
        date: new Date()
      },
      {
        title: "水上运动",
        description: "体验帆板、香蕉船等水上项目。",
        duration: "2小时",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "海底世界观光",
        description: "乘坐玻璃底船观赏海洋生物。",
        duration: "1小时",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "张家界国家森林公园",
    description: "以奇特的石英砂岩峰林地貌闻名，是《阿凡达》电影取景地之一。",
    imageUrl: "/images/destinations/zhangjiajie.jpg",
    metadata: {
      location: "湖南省张家界市武陵源区",
      bestTimeToVisit: "4月-10月",
      tips: "建议游览3-4天，准备舒适的徒步鞋，避免雨天游览。",
      category: "自然风光"
    },
    activities: [
      {
        title: "天门山玻璃栈道",
        description: "体验刺激的高空玻璃栈道。",
        duration: "1小时",
        date: new Date()
      },
      {
        title: "百龙电梯观光",
        description: "乘坐世界最高户外电梯。",
        duration: "30分钟",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "金鞭溪徒步",
        description: "漫步溪谷，观赏峰林地貌。",
        duration: "2.5小时",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "泰山",
    description: "中国五岳之首，历史文化与自然景观的完美结合。",
    imageUrl: "/images/destinations/taishan.jpg",
    metadata: {
      location: "山东省泰安市泰山区",
      bestTimeToVisit: "5月-10月",
      tips: "可以选择夜爬看日出，准备登山装备，量力而行。",
      category: "自然风光"
    },
    activities: [
      {
        title: "日观峰日出",
        description: "观赏泰山日出，体验云海奇观。",
        duration: "2小时",
        date: new Date()
      },
      {
        title: "天街游览",
        description: "参观泰山最热闹的商业街。",
        duration: "1.5小时",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "石刻艺术欣赏",
        description: "了解泰山石刻文化与历史。",
        duration: "1小时",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  },
  {
    name: "苏州园林",
    description: "世界文化遗产，展现了中国古典园林的精髓。",
    imageUrl: "/images/destinations/suzhou-gardens.jpg",
    metadata: {
      location: "江苏省苏州市姑苏区",
      bestTimeToVisit: "3月-5月，9月-11月",
      tips: "建议请导游讲解园林文化，避开节假日人流高峰。",
      category: "文化古迹"
    },
    activities: [
      {
        title: "拙政园游览",
        description: "参观苏州最大的古典园林。",
        duration: "2小时",
        date: new Date()
      },
      {
        title: "狮子林探访",
        description: "体验迷宫式山水园林。",
        duration: "1.5小时",
        date: new Date(Date.now() + 24 * 60 * 60 * 1000)
      },
      {
        title: "园林茶艺体验",
        description: "品茶赏园，感受园林文化。",
        duration: "1小时",
        date: new Date(Date.now() + 48 * 60 * 60 * 1000)
      }
    ]
  }
];

async function main() {
  // 创建一个系统用户
  const hashedPassword = await hash('system', 12);  // 使用 bcryptjs 加密密码
  
  const systemUser = await prisma.user.upsert({
    where: { username: 'system' },
    update: {},
    create: {
      username: 'system',
      password: hashedPassword,  // 使用加密后的密码
      name: '系统用户',
    },
  });

  // 清空现有的官方目的地
  await prisma.destination.deleteMany({
    where: {
      isOfficial: true
    }
  });

  // 使用已定义的 officialDestinations 数组
  for (const destData of officialDestinations) {
    // 首先创建目的地
    const destination = await prisma.destination.create({
      data: {
        name: destData.name,
        description: destData.description,
        imageUrl: destData.imageUrl,
        isOfficial: true,
        createdById: systemUser.id,
        metadata: destData.metadata,
      }
    });

    // 为目的地创建一个默认行程
    const trip = await prisma.trip.create({
      data: {
        title: `游览${destData.name}`,
        description: destData.description,
        startDate: new Date(),
        endDate: addDays(new Date(), 7),
        isOfficial: true,
        isPublic: true,
        userId: systemUser.id
      }
    });

    // 为每个目的地创建活动，并关联到行程
    for (const activity of destData.activities) {
      await prisma.activity.create({
        data: {
          title: activity.title,
          description: activity.description,
          date: activity.date,
          metadata: {
            duration: activity.duration
          },
          destinationId: destination.id,
          tripId: trip.id  // 添加必需的 tripId
        }
      });
    }

    console.log(`Created destination: ${destination.name} with ${destData.activities.length} activities`);
  }
}

main()
  .catch((e) => {
    console.error('Error:', e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  }); 